import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		list: [],
		dateList: [],
		doneList: [],
		expiredList: {
			dateList: [],
			expiredTasks: [],
		},
		// 是否显示修改按钮
		showButtons: false,
		//textarea是否可以输入
		disabled: false,
		// 从完成列表中跳转到edit页面传的参数
		lookDoneItem: false,
		// 从过期列表跳转到edit页面传的参数
		lookExpiredItem: false,
		// 这里记录从哪个页面跳转来的
		fromPage: ""
	},
	mutations: {
		addToList(state, payload) {
			state.list.unshift(payload)
			this.commit('saveTolocalStorage', {
				name: "list",
				data: state.list
			})
		},
		// 如果已经有了这个时间就不用再加进去了
		addTodateList(state, payload) {
			const targetTimeItem = state.dateList.find(item => {
				return item.date === payload
			})
			// 如果dateList已经有了这个日期，就将isAllDone这个值修改未false就行了
			if (targetTimeItem) {
				targetTimeItem.isAllDone = false
				this.commit('saveTolocalStorage', {
					name: "dateList",
					data: state.dateList
				})
				return
			} else {
				state.dateList.push({
					date: payload,
					isAllDone: false
				})
			}
			// 升序排序
			state.dateList = state.dateList.sort(function(a, b) {
				return a.date.localeCompare(b.date)
			})
			this.commit('saveTolocalStorage', {
				name: "dateList",
				data: state.dateList
			})
		},
		deleteListItem(state, payload) {
			const targetIndex = state.list.findIndex(item => {
				return item.id === payload.id
			})
			state.list.splice(targetIndex, 1)
			this.commit('saveTolocalStorage', {
				name: "list",
				data: state.list
			})
		},
		addToDoneList(state, payload) {
			state.doneList.unshift(payload)
			this.commit('saveTolocalStorage', {
				name: "doneList",
				data: state.doneList
			})
		},
		// 这是检查list中还有没有同日期未完成的任务，如果没有的话就将isAllDone这个值设为true
		checkList(state, payload) {
			const checkedList = state.list.filter(item => {
				return item.date === payload && !item.isDone
			})

			if (checkedList.length === 0) {
				const targetIndex = state.dateList.findIndex(item => {
					return item.date === payload
				})
				state.dateList[targetIndex]["isAllDone"] = true
				this.commit('saveTolocalStorage', {
					name: "dateList",
					data: state.dateList
				})
			}
		},
		// 删除任务的时候，当某个日期下的任务都被删完了，那么这个日期也应该删掉
		checkDateList(state, payload) {
			const list = state.list.filter(timeItem => {
				return timeItem.date === payload.date
			})
			if (list.length === 0) {
				const targetIndex = state.dateList.findIndex(item => {
					return item.date === payload.date
				})
				state.dateList.splice(targetIndex, 1)
				this.commit('saveTolocalStorage', {
					name: "dateList",
					data: state.dateList
				})
			}
		},
		// 修改状态
		ModifyStatus(state, {
			showButtons,
			disabled,
			lookDoneItem,
			lookExpiredItem
		}) {
			// 这里之所要存在到本地是因为在原地刷新页面的时候需要用到这些参数
			state.showButtons = showButtons
			this.commit('saveTolocalStorage', {
				name: 'showButtons',
				data: showButtons
			})
			state.disabled = disabled
			this.commit('saveTolocalStorage', {
				name: 'disabled',
				data: disabled
			})
			state.lookDoneItem = lookDoneItem
			this.commit('saveTolocalStorage', {
				name: 'lookDoneItem',
				data: lookDoneItem
			})
			state.lookExpiredItem = lookExpiredItem
			this.commit('saveTolocalStorage', {
				name: 'lookExpiredItem',
				data: lookExpiredItem
			})
		},
		// 修改内容
		ModifyContent(state, payload) {
			const targetItem = state.list.find(function(item) {
				return item.id === payload.id
			})
			targetItem["content"] = payload["content"]
			this.commit('saveTolocalStorage', {
				name: "list",
				data: state.list
			})
		},
		// 加到expiredList中
		addToExpiredList(state, {
			timeItem,
			taskItem
		}) {
			const targetIndex = state.expiredList['dateList'].findIndex(item => {
				return item.date === timeItem.date
			})
			if (targetIndex < 0) {
				state.expiredList['dateList'].push(timeItem)
				state.expiredList['dateList'] = state.expiredList['dateList'].sort(function(a, b) {
					return a.date.localeCompare(b.date)
				})
			}
			state.expiredList['expiredTasks'].push(taskItem)
			this.commit('saveTolocalStorage', {
				name: "expiredList",
				data: state.expiredList
			})
		},
		// 过滤出过期任务和日期
		filterExpiredList(state, payload) {
			if (state.list.length > 0) {
				state.list.forEach((item, index) => {
					if (item.date < payload) {
						const targetIndex = state.dateList.findIndex(timeItem => {
							return timeItem.date === item.date
						})
						this.commit('addToExpiredList', {
							timeItem: state.dateList[targetIndex],
							taskItem: item
						})

						state.list.splice(index, 1)
						state.dateList.splice(targetIndex, 1)
						this.commit('saveTolocalStorage', {
							name: "list",
							data: state.list
						})
						this.commit('saveTolocalStorage', {
							name: "dateList",
							data: state.dateList
						})
					}
				})
			}
		},
		// 保存fromPage
		saveFromPage(state, payload) {
			state.fromPage = payload
			this.commit('saveTolocalStorage', {
				name: "fromPage",
				data: payload
			})
		},
		saveTolocalStorage(state, {
			name,
			data
		}) {
			uni.setStorageSync(name, JSON.stringify(data))
		},
		getLocalStorage(state, payload) {
			if (uni.getStorageSync('dateList')) {
				state.dateList = JSON.parse(uni.getStorageSync('dateList'))
			}
			if (uni.getStorageSync('list')) {
				state.list = JSON.parse(uni.getStorageSync('list'))
			}
			if (uni.getStorageSync('doneList')) {
				state.doneList = JSON.parse(uni.getStorageSync('doneList'))
			}
			if (uni.getStorageSync('expiredList')) {
				state.expiredList = JSON.parse(uni.getStorageSync('expiredList'))
			}
			if (uni.getStorageSync('showButtons')) {
				state.showButtons = JSON.parse(uni.getStorageSync('showButtons'))
			}
			if (uni.getStorageSync('disabled')) {
				state.disabled = JSON.parse(uni.getStorageSync('disabled'))
			}
			if (uni.getStorageSync('lookDoneItem')) {
				state.lookDoneItem = JSON.parse(uni.getStorageSync('lookDoneItem'))
			}
			if (uni.getStorageSync('lookExpiredItem')) {
				state.lookExpiredItem = JSON.parse(uni.getStorageSync('lookExpiredItem'))
			}
			if (uni.getStorageSync('fromPage')) {
				state.fromPage = JSON.parse(uni.getStorageSync('fromPage'))
			}
		},
		// 删除本地存储中的记录
		removeRecord(state, {
			name,
			date
		}) {
			if (name === 'expiredList') {
				state.expiredList['dateList'] = []
				state.expiredList['expiredTasks'] = []
				uni.removeStorageSync(name)
			} else if (name === 'doneList') {
				// for (let i = 0; i < state.doneList.length; i++) {
				// 	if (state.doneList[i].date === date) {
				// 		state.doneList.splice(i, 1)
				// 		i--
				// 	}
				// }
				for (let i = state.doneList.length-1; i >= 0; i--) {
					if (state.doneList[i].date === date) {
						state.doneList.splice(i, 1)
					}
				}
				this.commit('saveTolocalStorage', {
					name: 'doneList',
					data: state.doneList
				})

			}
		}
	},
	actions: {

	}
})
export default store
